<?php
class ControllerRestaurantRestaurant extends Controller {
	private $error = array(); 

	public function index() {
		$this->load->language('common/header');
		$this->load->language('restaurant/restaurant');

		$this->document->setTitle($this->language->get('heading_title'));
		 
		$this->load->model('restaurant/restaurant');

		$this->getList();
	}
	      
  	public function insert() {
		$this->load->language('common/header');
  		$this->load->language('restaurant/restaurant');

    	$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->load->model('restaurant/restaurant');
		
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$restaurant_id = $this->model_restaurant_restaurant->addRestaurant($this->request->post);
	  		
			$this->load->model('restaurant/setting');
			
			$this->model_restaurant_setting->editSetting('config', $this->request->post, $restaurant_id);
			
			$this->session->data['success'] = $this->language->get('text_success');
			
//			$this->redirect($this->url->link('restaurant/restaurant', 'token=' . $this->session->data['token'], 'SSL'));
			$this->redirect($this->url->link('restaurant/restaurant/update', 'token=' . $this->session->data['token'] . '&restaurant_id=' . $restaurant_id , 'SSL'));
    	}
	
    	$this->getForm();
  	}

  	public function update() {
		$this->load->language('common/header');
  		$this->load->language('restaurant/restaurant');

    	$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('restaurant/restaurant');
	
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_restaurant_restaurant->editRestaurant($this->request->get['restaurant_id'], $this->request->post);
			
			$this->load->model('restaurant/setting');
			
			$this->model_restaurant_setting->editSetting('config', $this->request->post, $this->request->get['restaurant_id']);
			
			$this->session->data['success'] = $this->language->get('text_success');
			
			$this->redirect($this->url->link('restaurant/restaurant/update', 'token=' . $this->session->data['token'] . '&restaurant_id=' . $this->request->get['restaurant_id'], 'SSL'));
		}

    	$this->getForm();
  	}

  	public function delete() {
		$this->load->language('common/header');
  		$this->load->language('restaurant/restaurant');

    	$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('restaurant/restaurant');
		
		$this->load->model('restaurant/setting');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $restaurant_id) {
				$this->model_restaurant_restaurant->deleteRestaurant($restaurant_id);
				
				$this->model_restaurant_setting->deleteSetting('config', $restaurant_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');
			
			$this->redirect($this->url->link('restaurant/restaurant', 'token=' . $this->session->data['token'], 'SSL'));
		}

    	$this->getList();
  	}
	
	private function getList() {
		$url = '';
			
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_restaurant'),
			'href'      => '#',
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => '#',
      		'separator' => ' :: '
   		);
							
		$this->data['insert'] = $this->url->link('restaurant/restaurant/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['delete'] = $this->url->link('restaurant/restaurant/delete', 'token=' . $this->session->data['token'], 'SSL');	

		$this->data['restaurants'] = array();
		
		$action = array();
					
		$action[] = array(
			'text' => $this->language->get('text_edit'),
			'href' => $this->url->link('restaurant/setting', 'token=' . $this->session->data['token'], 'SSL')
		);
							
		$restaurant_total = $this->model_restaurant_restaurant->getTotalRestaurants();
	
		$results = $this->model_restaurant_restaurant->getRestaurants();
 
    	foreach ($results as $result) {
			$action = array();
						
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('restaurant/restaurant/update', 'token=' . $this->session->data['token'] . '&restaurant_id=' . $result['restaurant_id'], 'SSL')
			);
						
			$this->data['restaurants'][] = array(
				'restaurant_id' => $result['restaurant_id'],
				'name'     => $result['name'],
				'url'      => $result['url'],
				'status'   => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'selected' => isset($this->request->post['selected']) && in_array($result['restaurant_id'], $this->request->post['selected']),
				'action'   => $action
			);
		}	
	
		// Mapping all language variables
		$this->data = array_merge($this->data, $this->language->getData());
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$this->template = 'restaurant/restaurant_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	 
	public function getForm() { 
		// Mapping all language variables
		$this->data = array_merge($this->data, $this->language->getData());
		
		$this->data['token'] = $this->session->data['token'];
		
		$this->data['restaurant_id'] = isset($this->request->get['restaurant_id'])?$this->request->get['restaurant_id']:$this->restaurant->getRestaurantId();
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
 		if (isset($this->error['url'])) {
			$this->data['error_url'] = $this->error['url'];
		} else {
			$this->data['error_url'] = '';
		}

 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = '';
		}
		
 		if (isset($this->error['owner'])) {
			$this->data['error_owner'] = $this->error['owner'];
		} else {
			$this->data['error_owner'] = '';
		}

		if (isset($this->error['require_address'])) {
			$this->data['error_require_address'] = $this->error['require_address'];
		} else {
			$this->data['error_require_address'] = '';
		}
		
 		if (isset($this->error['address'])) {
			$this->data['error_address'] = $this->error['address'];
		} else {
			$this->data['error_address'] = '';
		}
		
 		if (isset($this->error['email'])) {
			$this->data['error_email'] = $this->error['email'];
		} else {
			$this->data['error_email'] = '';
		}

		if (isset($this->error['telephone'])) {
			$this->data['error_telephone'] = $this->error['telephone'];
		} else {
			$this->data['error_telephone'] = '';
		}
		
 		if (isset($this->error['title'])) {
			$this->data['error_title'] = $this->error['title'];
		} else {
			$this->data['error_title'] = '';
		}
	
 		if (isset($this->error['district'])) {
			$this->data['error_district'] = $this->error['district'];
		} else {
			$this->data['error_district'] = '';
		}
		
 		if (isset($this->error['image_category'])) {
			$this->data['error_image_category'] = $this->error['image_category'];
		} else {
			$this->data['error_image_category'] = '';
		}
		
 		if (isset($this->error['image_logo'])) {
			$this->data['error_image_logo'] = $this->error['image_logo'];
		} else {
			$this->data['error_image_logo'] = '';
		}
		
 		if (isset($this->error['image_thumb'])) {
			$this->data['error_image_thumb'] = $this->error['image_thumb'];
		} else {
			$this->data['error_image_thumb'] = '';
		}
		
 		if (isset($this->error['image_popup'])) {
			$this->data['error_image_popup'] = $this->error['image_popup'];
		} else {
			$this->data['error_image_popup'] = '';
		}
		
 		if (isset($this->error['image_food'])) {
			$this->data['error_image_food'] = $this->error['image_food'];
		} else {
			$this->data['error_image_food'] = '';
		}
				
 		if (isset($this->error['image_additional'])) {
			$this->data['error_image_additional'] = $this->error['image_additional'];
		} else {
			$this->data['error_image_additional'] = '';
		}	
		
 		if (isset($this->error['image_related'])) {
			$this->data['error_image_related'] = $this->error['image_related'];
		} else {
			$this->data['error_image_related'] = '';
		}

		if (isset($this->error['catalog_limit'])) {
			$this->data['error_catalog_limit'] = $this->error['catalog_limit'];
		} else {
			$this->data['error_catalog_limit'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		
   		$this->data['breadcrumbs'][] = array(
   			'text'      => $this->language->get('text_restaurant'),
   			'href'      => '',
	   		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
   			'text'      => $this->language->get('text_restaurant'),
   			'href'      => $this->url->link('restaurant/restaurant', 'token=' . $this->session->data['token'], 'SSL'),
   			'separator' => ' :: '
   		);
   		   		
   		if (!isset($this->data['restaurant_id'])) {
	   		$this->data['breadcrumbs'][] = array(
	       		'text'      => $this->language->get('heading_title_insert'),
				'href'      => '',
	      		'separator' => ' :: '
	   		);
   		} else {
   			$this->data['breadcrumbs'][] = array(
	       		'text'      => $this->language->get('heading_title_update'),
				'href'      => '',
	      		'separator' => ' :: '
	   		);
   		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		if (!isset($this->data['restaurant_id'])) {
			$this->data['action'] = $this->url->link('restaurant/restaurant/insert', 'token=' . $this->session->data['token'], 'SSL');
		} else {
			$this->data['action'] = $this->url->link('restaurant/restaurant/update', 'token=' . $this->session->data['token'] . '&restaurant_id=' . $this->data['restaurant_id'], 'SSL');
		}
				
		$this->data['cancel'] = $this->url->link('restaurant/restaurant', 'token=' . $this->session->data['token'], 'SSL');
	
		if (isset($this->data['restaurant_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$this->load->model('restaurant/setting');
			
			$restaurant_info = $this->model_restaurant_restaurant->getRestaurant($this->data['restaurant_id']);
			
      		$restaurant_info_config = $this->model_restaurant_setting->getSetting('config', $this->data['restaurant_id']);
      		
      		$restaurant_info = array_merge($restaurant_info, $restaurant_info_config);
    	}
    	
		if (isset($this->request->post['description'])) {
			$this->data['description'] = $this->request->post['description'];
		} elseif (isset($this->request->get['restaurant_id'])) {
			$this->data['description'] = $this->model_restaurant_restaurant->getRestaurantDescriptions($this->request->get['restaurant_id']);
		} else {
			$this->data['description'] = array();
		}
	
		if (isset($this->request->post['addresses'])) {
			$this->data['addresses'] = $this->request->post['addresses'];
		} elseif (isset($this->request->get['restaurant_id'])) {
			$this->data['addresses'] = $this->model_restaurant_restaurant->getRestaurantAddresses($this->request->get['restaurant_id']);
		} else {
			$this->data['addresses'] = array();
		}
		
		if (isset($this->request->post['config_url'])) {
			$this->data['config_url'] = $this->request->post['config_url'];
		} elseif (isset($restaurant_info['config_url'])) {
			$this->data['config_url'] = $restaurant_info['config_url'];
		} else {
			$this->data['config_url'] = '';
		}
		
		if (isset($this->request->post['config_ssl'])) {
			$this->data['config_ssl'] = $this->request->post['config_ssl'];
		} elseif (isset($restaurant_info['config_ssl'])) {
			$this->data['config_ssl'] = $restaurant_info['config_ssl'];
		} else {
			$this->data['config_ssl'] = '';
		}
	
		if (isset($this->request->post['keyword'])) {
			$this->data['keyword'] = $this->request->post['keyword'];
		} elseif (!empty($restaurant_info)) {
			$this->data['keyword'] = $restaurant_info['keyword'];
		} else {
			$this->data['keyword'] = '';
		}

		if (isset($this->request->post['restaurant_tag'])) {
			$this->data['restaurant_tag'] = $this->request->post['restaurant_tag'];
		} elseif (isset($this->request->get['restaurant_id'])) {
			$this->data['restaurant_tag'] = $this->model_restaurant_restaurant->getRestaurantTags($this->request->get['restaurant_id']);
		} else {
			$this->data['restaurant_tag'] = array();
		}
		
		if (isset($this->request->post['image'])) {
			$this->data['image'] = $this->request->post['image'];
		} elseif (!empty($restaurant_info)) {
			$this->data['image'] = $restaurant_info['image'];
		} else {
			$this->data['image'] = '';
		}
		
		$this->load->model('setting/image');

		if (!empty($this->data['image']) && file_exists(DIR_IMAGE . $this->data['image'])) {
			$this->data['thumb'] = $this->model_setting_image->resize($this->data['image'], 100, 100);
		} else {
			$this->data['thumb'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);
		}
						
		if (isset($this->request->post['more_image'])) {
			$more_images = $this->request->post['more_image'];
		} elseif (isset($this->request->get['restaurant_id'])) {
			$more_images = $this->model_restaurant_restaurant->getRestaurantImages($this->request->get['restaurant_id']);
		} else {
			$more_images = array();
		}
		
		$this->data['more_images'] = array();
		
		foreach ($more_images as $more_image) {
			if ($more_image['image'] && file_exists(DIR_IMAGE . $more_image['image'])) {
				$image = $more_image['image'];
			} else {
				$image = 'no_image.jpg';
			}
			
			$this->data['more_images'][] = array(
				'image'      => $image,
				'thumb'      => $this->model_setting_image->resize($image, 100, 100),
				'sort_order' => $more_image['sort_order'],
			);
		}
		
		$this->data['no_image'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);

		if (isset($this->request->post['config_owner'])) {
			$this->data['config_owner'] = $this->request->post['config_owner'];
		} elseif (isset($restaurant_info['config_owner'])) {
			$this->data['config_owner'] = $restaurant_info['config_owner'];		
		} else {
			$this->data['config_owner'] = '';
		}
		
		if (isset($this->request->post['config_email'])) {
			$this->data['config_email'] = $this->request->post['config_email'];
		} elseif (isset($restaurant_info['config_email'])) {
			$this->data['config_email'] = $restaurant_info['config_email'];		
		} else {
			$this->data['config_email'] = '';
		}
		
		if (isset($this->request->post['config_telephone'])) {
			$this->data['config_telephone'] = $this->request->post['config_telephone'];
		} elseif (isset($restaurant_info['config_telephone'])) {
			$this->data['config_telephone'] = $restaurant_info['config_telephone'];		
		} else {
			$this->data['config_telephone'] = '';
		}

		if (isset($this->request->post['config_fax'])) {
			$this->data['config_fax'] = $this->request->post['config_fax'];
		} elseif (isset($restaurant_info['config_fax'])) {
			$this->data['config_fax'] = $restaurant_info['config_fax'];		
		} else {
			$this->data['config_fax'] = '';
		}

		if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
		} elseif (isset($restaurant_info['status'])) {
			$this->data['status'] = $restaurant_info['status'];		
		} else {
			$this->data['status'] = '';
		}

		if (isset($this->request->post['restaurant_option'])) {
			$this->data['restaurant_option'] = $this->request->post['restaurant_option'];
		} elseif (isset($restaurant_info)) {
			$this->data['restaurant_option'] = $this->model_restaurant_restaurant->getRestaurantOptions($restaurant_info['restaurant_id']);		
		} else {
			$this->data['restaurant_option'] = '';
		}
		
		if (isset($this->request->post['config_title'])) {
			$this->data['config_title'] = $this->request->post['config_title'];
		} elseif (isset($restaurant_info['config_title'])) {
			$this->data['config_title'] = $restaurant_info['config_title'];
		} else {
			$this->data['config_title'] = '';
		}
		
		if (isset($this->request->post['config_template'])) {
			$this->data['config_template'] = $this->request->post['config_template'];
		} elseif (isset($restaurant_info['config_template'])) {
			$this->data['config_template'] = $restaurant_info['config_template'];
		} else {
			$this->data['config_template'] = '';
		}
		
		// Get restaurant config options
		$this->load->model('menu/option');
		$this->data['options'] = array();
		
		$data = array(
			'group'  => 'restaurant',
			'sort'  => 'od.name',
			'order' => 'ASC'
		);
		$results = $this->model_menu_option->getOptions($data);
		
		$this->load->model('setting/image');
		
		foreach ($results as $result) {
			$option = array(
				'option_id'  => $result['option_id'],
				'name'       => $result['name'],
				'type'       => $result['type'],
				'values'	 => array()
			);
			
			$option_results = $this->model_menu_option->getOptionValues($result['option_id']);
			foreach ($option_results as $option_result) {			
				if ($option_result['image'] && file_exists(DIR_IMAGE . $option_result['image'])) {
					$image = $this->model_setting_image->resize($option_result['image'], 40, 40);
				} else {
					$image = $this->model_setting_image->resize('no_image.jpg', 40, 40);
				}
				
				$option['values'][] = array(
					'option_value_id'	=> $option_result['option_value_id'],
					'name'			    => $option_result['name'],
					'image'			    => $image
				);
			}
			
			$this->data['options'][] = $option;
		}
		
		// Get all available templates
		$this->load->model('setting/template');
		$this->data['templates'] = array();

		// Get all public templates first
		$data = array(
			'filter_type'	  => '0',
			'filter_status'   => '1'
		);
		
		$results = $this->model_setting_template->getTemplates($data);
		
		foreach ($results as $result) {
			$this->data['templates'][] = array(
				'template_id' => $result['template_id'],
				'name'        => $result['name']
			);
		}
		
		if(isset($this->data['restaurant_id'])) {
			// Get all private templates
			$data = array(
				'filter_type'	  => '1',
				'filter_status'   => '1',
				'filter_restaurant_id' => $this->data['restaurant_id']
			);
			
			$results = $this->model_setting_template->getTemplates($data);
			
			foreach ($results as $result) {
				$this->data['templates'][] = array(
					'template_id' => $result['template_id'],
					'name'        => $result['name']
				);
			}
		}
		
		$this->load->model('setting/country');
		
		$data = array('status' => 1);
		$this->data['countries'] = $this->model_setting_country->getCountries($data);
		
		if (isset($this->request->post['language'])) {
			$this->data['language'] = $this->request->post['language'];
		} elseif (isset($restaurant_info['language'])) {
			$this->data['language'] = $restaurant_info['language'];			
		} else {
			$this->data['language'] = $this->config->get('language');
		}
		
		$this->load->model('setting/language');
		
		$this->data['languages'] = $this->model_setting_language->getLanguages(array('portal' => '1'));

		if (isset($this->request->post['currency'])) {
			$this->data['currency'] = $this->request->post['currency'];
		} elseif (isset($restaurant_info['currency'])) {
			$this->data['currency'] = $restaurant_info['currency'];			
		} else {
			$this->data['currency'] = $this->config->get('currency');
		}
		
		$this->load->model('setting/currency');
		
		$this->data['currencies'] = $this->model_setting_currency->getCurrencies();
		
		if (isset($this->request->post['config_catalog_limit'])) {
			$this->data['config_catalog_limit'] = $this->request->post['config_catalog_limit'];
		} elseif (isset($restaurant_info['config_catalog_limit'])) {
			$this->data['config_catalog_limit'] = $restaurant_info['config_catalog_limit'];	
		} else {
			$this->data['config_catalog_limit'] = '12';
		}		
				
		$this->load->model('setting/image');

		if (isset($this->request->post['config_logo'])) {
			$this->data['config_logo'] = $this->request->post['config_logo'];
		} elseif (isset($restaurant_info['config_logo'])) {
			$this->data['config_logo'] = $restaurant_info['config_logo'];			
		} else {
			$this->data['config_logo'] = '';
		}

		if (isset($restaurant_info['config_logo']) && file_exists(DIR_IMAGE . $restaurant_info['config_logo']) && is_file(DIR_IMAGE . $restaurant_info['config_logo'])) {
			$this->data['logo'] = $this->model_setting_image->resize($restaurant_info['config_logo'], 100, 100);		
		} else {
			$this->data['logo'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);
		}

		if (isset($this->request->post['config_icon'])) {
			$this->data['config_icon'] = $this->request->post['config_icon'];
		} elseif (isset($restaurant_info['config_icon'])) {
			$this->data['config_icon'] = $restaurant_info['config_icon'];			
		} else {
			$this->data['config_icon'] = '';
		}
		
		if (isset($this->request->post['config_image_logo_height'])) {
			$this->data['config_image_logo_height'] = $this->request->post['config_image_logo_height'];
		} elseif (isset($restaurant_info['config_image_logo_height'])) {
			$this->data['config_image_logo_height'] = $restaurant_info['config_image_logo_height'];
		} else {
			$this->data['config_image_logo_height'] = 80;
		}
		
		if (isset($this->request->post['config_image_logo_width'])) {
			$this->data['config_image_logo_width'] = $this->request->post['config_image_logo_width'];
		} elseif (isset($restaurant_info['config_image_logo_width'])) {
			$this->data['config_image_logo_width'] = $restaurant_info['config_image_logo_width'];
		} else {
			$this->data['config_image_logo_width'] = 80;
		}
		
		if (isset($restaurant_info['config_icon']) && file_exists(DIR_IMAGE . $restaurant_info['config_icon']) && is_file(DIR_IMAGE . $restaurant_info['config_icon'])) {
			$this->data['icon'] = $this->model_setting_image->resize($restaurant_info['config_icon'], 100, 100);
		} else {
			$this->data['icon'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);
		}
		
		$this->data['no_image'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);
		
		if (isset($this->request->post['config_image_category_height'])) {
			$this->data['config_image_category_height'] = $this->request->post['config_image_category_height'];
		} elseif (isset($restaurant_info['config_image_category_height'])) {
			$this->data['config_image_category_height'] = $restaurant_info['config_image_category_height'];			
		} else {
			$this->data['config_image_category_height'] = 80;
		}	
				
		if (isset($this->request->post['config_image_thumb_width'])) {
			$this->data['config_image_thumb_width'] = $this->request->post['config_image_thumb_width'];
		} elseif (isset($restaurant_info['config_image_thumb_width'])) {
			$this->data['config_image_thumb_width'] = $restaurant_info['config_image_thumb_width'];			
		} else {
			$this->data['config_image_thumb_width'] = 228;
		}
		
		if (isset($this->request->post['config_image_thumb_height'])) {
			$this->data['config_image_thumb_height'] = $this->request->post['config_image_thumb_height'];
		} elseif (isset($restaurant_info['config_image_thumb_height'])) {
			$this->data['config_image_thumb_height'] = $restaurant_info['config_image_thumb_height'];				
		} else {
			$this->data['config_image_thumb_height'] = 228;
		}
		
		if (isset($this->request->post['config_image_popup_width'])) {
			$this->data['config_image_popup_width'] = $this->request->post['config_image_popup_width'];
		} elseif (isset($restaurant_info['config_image_popup_width'])) {
			$this->data['config_image_popup_width'] = $restaurant_info['config_image_popup_width'];			
		} else {
			$this->data['config_image_popup_width'] = 500;
		}
		
		if (isset($this->request->post['config_image_popup_height'])) {
			$this->data['config_image_popup_height'] = $this->request->post['config_image_popup_height'];
		} elseif (isset($restaurant_info['config_image_popup_height'])) {
			$this->data['config_image_popup_height'] = $restaurant_info['config_image_popup_height'];			
		} else {
			$this->data['config_image_popup_height'] = 500;
		}
		
		if (isset($this->request->post['config_image_food_width'])) {
			$this->data['config_image_food_width'] = $this->request->post['config_image_food_width'];
		} elseif (isset($restaurant_info['config_image_food_width'])) {
			$this->data['config_image_food_width'] = $restaurant_info['config_image_food_width'];		
		} else {
			$this->data['config_image_food_width'] = 80;
		}
		
		if (isset($this->request->post['config_image_food_height'])) {
			$this->data['config_image_food_height'] = $this->request->post['config_image_food_height'];
		} elseif (isset($restaurant_info['config_image_food_height'])) {
			$this->data['config_image_food_height'] = $restaurant_info['config_image_food_height'];		
		} else {
			$this->data['config_image_food_height'] = 80;
		}
		
		if (isset($this->request->post['config_image_category_width'])) {
			$this->data['config_image_category_width'] = $this->request->post['config_image_category_width'];
		} elseif (isset($restaurant_info['config_image_category_width'])) {
			$this->data['config_image_category_width'] = $restaurant_info['config_image_category_width'];			
		} else {
			$this->data['config_image_category_width'] = 80;
		}

		if (isset($this->request->post['config_image_additional_width'])) {
			$this->data['config_image_additional_width'] = $this->request->post['config_image_additional_width'];
		} elseif (isset($restaurant_info['config_image_additional_width'])) {
			$this->data['config_image_additional_width'] = $restaurant_info['config_image_additional_width'];			
		} else {
			$this->data['config_image_additional_width'] = 74;
		}
		
		if (isset($this->request->post['config_image_additional_height'])) {
			$this->data['config_image_additional_height'] = $this->request->post['config_image_additional_height'];
		} elseif (isset($restaurant_info['config_image_additional_height'])) {
			$this->data['config_image_additional_height'] = $restaurant_info['config_image_additional_height'];				
		} else {
			$this->data['config_image_additional_height'] = 74;
		}
		
		if (isset($this->request->post['config_image_related_width'])) {
			$this->data['config_image_related_width'] = $this->request->post['config_image_related_width'];
		} elseif (isset($restaurant_info['config_image_related_width'])) {
			$this->data['config_image_related_width'] = $restaurant_info['config_image_related_width'];		
		} else {
			$this->data['config_image_related_width'] = 80;
		}
		
		if (isset($this->request->post['config_image_related_height'])) {
			$this->data['config_image_related_height'] = $this->request->post['config_image_related_height'];
		} elseif (isset($restaurant_info['config_image_related_height'])) {
			$this->data['config_image_related_height'] = $restaurant_info['config_image_related_height'];			
		} else {
			$this->data['config_image_related_height'] = 80;
		}

		$this->template = 'restaurant/restaurant_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'restaurant/restaurant')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->request->post['config_url']) {
			$this->error['url'] = $this->language->get('error_url');
		}
	
		foreach ($this->request->post['description'] as $language_id => $value) {
			if ((utf8_strlen($value['name']) < 2) || (utf8_strlen($value['name']) > 255)) {
				$this->error['name'][$language_id] = $this->language->get('error_name');
			}
		}
		
		if ((utf8_strlen($this->request->post['config_owner']) < 3) || (utf8_strlen($this->request->post['config_owner']) > 64)) {
			$this->error['owner'] = $this->language->get('error_owner');
		}
		
    	if ((utf8_strlen($this->request->post['config_email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['config_email'])) {
      		$this->error['email'] = $this->language->get('error_email');
    	}

    	if ((utf8_strlen($this->request->post['config_telephone']) < 3) || (utf8_strlen($this->request->post['config_telephone']) > 32)) {
      		$this->error['telephone'] = $this->language->get('error_telephone');
    	}
		
		if (!$this->request->post['config_title']) {
			$this->error['title'] = $this->language->get('error_title');
		}	
	
		if(!isset($this->request->post['addresses']) || sizeof($this->request->post['addresses']) == 0){
			$this->error['require_address'] = $this->language->get('error_require_address');
		} else {		
			$i = 0;
			foreach ($this->request->post['addresses'] as $address) {
				if (!$address['district_id'] || empty($address['district_id'])) {
					$this->error['district'][$i] = $this->language->get('error_district');
				}
				
				if ((utf8_strlen($address['address']) < 3) || (utf8_strlen($address['address']) > 256)) {
					$this->error['address'][$i] = $this->language->get('error_address');
				}
				
				$i++;
			}
		}
				
		if (!$this->request->post['config_image_category_width'] || !$this->request->post['config_image_category_height']) {
			$this->error['image_category'] = $this->language->get('error_image_category');
		}
				
		if (!$this->request->post['config_image_logo_width'] || !$this->request->post['config_image_logo_height']) {
			$this->error['image_logo'] = $this->language->get('error_image_logo');
		}
		
		if (!$this->request->post['config_image_thumb_width'] || !$this->request->post['config_image_thumb_height']) {
			$this->error['image_thumb'] = $this->language->get('error_image_thumb');
		}	
		
		if (!$this->request->post['config_image_popup_width'] || !$this->request->post['config_image_popup_height']) {
			$this->error['image_popup'] = $this->language->get('error_image_popup');
		}
			
		if (!$this->request->post['config_image_food_width'] || !$this->request->post['config_image_food_height']) {
			$this->error['image_food'] = $this->language->get('error_image_food');
		}
		
		if (!$this->request->post['config_image_additional_width'] || !$this->request->post['config_image_additional_height']) {
			$this->error['image_additional'] = $this->language->get('error_image_additional');
		}
		
		if (!$this->request->post['config_image_related_width'] || !$this->request->post['config_image_related_height']) {
			$this->error['image_related'] = $this->language->get('error_image_related');
		}
		
		if (!$this->request->post['config_catalog_limit']) {
			$this->error['catalog_limit'] = $this->language->get('error_limit');
		}
		
		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}
					
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'restaurant/restaurant')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		$this->load->model('sale/order');
		
		foreach ($this->request->post['selected'] as $restaurant_id) {
			if (!$restaurant_id) {
				$this->error['warning'] = $this->language->get('error_default');
			}
			
			$restaurant_total = $this->model_sale_order->getTotalOrdersByRestaurantId($restaurant_id);
	
			if ($restaurant_total) {
				$this->error['warning'] = sprintf($this->language->get('error_restaurant'), $restaurant_total);
			}	
		}
		
		if (!$this->error) {
			return true; 
		} else {
			return false;
		}
	}
}
?>